﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL.crm;
using Gift.Model.crm;
using Gift.Model.crm.eNum;

namespace WebSite.crm.order
{
    public partial class goodsInDetail : BaseCrm
    {
        readonly t_DALWarehouseGoods theWarehouseGoods = new t_DALWarehouseGoods();
        readonly t_DALBrand theBrand = new t_DALBrand();
        readonly t_DALCompany theCompany = new t_DALCompany();
        t_WarehouseGoods oWarehouseGoods;
        t_Brand oBrand;
        readonly t_DALGoods theGoods = new t_DALGoods();
        t_Goods oGoods;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(eRule.GoodsIn);
                this.BindCompanyList();
                this.BuildForm();

            }
        }

        void BindCompanyList()
        {
            DataTable table = theCompany.GetList("companyTypeId in(1,2)").Tables[0];
            this.ddlCompanyList.DataTextField = "id";
            this.ddlCompanyList.DataValueField = "Name";
            this.ddlCompanyList.DataSource = table;
            this.ddlCompanyList.DataBind();
            this.ddlCompanyList.Items.Insert(0, new ExtAspNet.ListItem("选择供应商", "-1"));
            this.ddlCompanyList.SelectedValue = "-1";
        }




    
        void BuildForm()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["gid"]))
            {
                oGoods = theGoods.GetModel(int.Parse(Request.QueryString["gid"]));
                if (oGoods != null)
                {
                    this.txtGoodsCode.Text = oGoods.goodsCode;
                    this.lblGoodsName.Label = "商品名称";
                    oBrand = theBrand.GetModel((int)oGoods.brandId);
                    this.lblGoodsName.Text = oBrand.brandName + "-" +  oGoods.goodsName;
                }
            }
        }
   


        protected void btnSubmitForm1_Click(object sender, EventArgs e)
        {
            oGoods = theGoods.GetModelByGoodsCode(this.txtGoodsCode.Text.Trim());
            if (oGoods != null)
            {
                oWarehouseGoods = new t_WarehouseGoods();

                oWarehouseGoods.creater = int.Parse(base.LoginAdminId);
                oWarehouseGoods.createTime = DateTime.Now;
                oWarehouseGoods.goodsCount = int.Parse(this.txtCount.Text);
                oWarehouseGoods.goodsId = oGoods.goodsId;
                oWarehouseGoods.remark = this.txtRemark.Text.Trim();
                oWarehouseGoods.companyId = int.Parse(this.ddlCompanyList.SelectedItem.Value);
                oWarehouseGoods.goodsOrderCode = this.txtStockOrderCode.Text.Trim();
                oWarehouseGoods.costPrice = decimal.Parse(this.txtCostPrice.Text);
           
                if (theWarehouseGoods.Add(oWarehouseGoods) > 0)
                {
                    ExtAspNet.Alert.Show("添加成功！", "", "window.location.href='GoodsInDetail.aspx'");
                }
            }
            else
            {
                ExtAspNet.Alert.Show("没有找到商品编号！");
            }
        }
    }
}